/* this file defines tables that associates a number and an entry point to an user program. It also defines functions to access them.  */

#include "user_programs.h"

/* This array associates a program with an integer (the index) which will be used to retrieve the entry point in the second array. This integer is the program_id saved into the PCB */

static char* program_names[] = {"count","fibonacci","uart_test", "increment", "supervised_spawn", "supervise", "scroller","shell","ring","ring_node"};

/* This array matches the program_id with the entry point of the user program */

static uint32_t entry_points[] = {(uint32_t)count,(uint32_t)fibonacci,(int32_t)uart_test,(int32_t)increment,(int32_t)supervised_spawn,(int32_t)supervise,(int32_t)scroller,(int32_t)shell,(int32_t)ring,(int32_t)ring_node};


/* returns the program id of the program named program_name */
int32_t get_program_id(char* program_name){
  int i;
  for(i=0;i < COUNT_UP && strcmp(program_name,program_names[i]) ;i++);
  // either i equals COUNT_UP and we didn't find the requested UP or i is the program id 
  if(i == COUNT_UP)
    return -1; //-1 indicates an error  
  return i;
}

/* returns the entry point of the program with id as program id (as defined in the tables). Returns 0 in case of failure. */
uint32_t get_entry_point(int32_t program_id){
  if(program_id >= COUNT_UP)
    return 0;
  return entry_points[program_id];
}

/* copy the program name of the program having program_id as id in the program_name buffer (up to n characters). Returns 1 if success, 0 otherwise. */
int32_t get_program_name(int32_t program_id, char* program_name, int32_t n){
  if(program_id >= COUNT_UP)
    return 0;
  strncpy(program_name,program_names[program_id],n);
  return 1;
}





